package com.arch.bigdata.hbase;

import com.google.common.collect.Lists;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @author: pizhihui
 * @date: 2019-11-25
 */
public class HbaseUtils {

    private static final String VERTICAL_LINE = "|";

    private static final String FAMILY_NAME = "label";


    public static void main(String[] args) throws IOException {

        Configuration hbaseConf = HBaseConfiguration.create();

        hbaseConf.set("hbase.zookeeper.quorum", "10.16.16.139:2181,10.16.104.8:2181,10.16.16.106:2181");

        TableName tableName = TableName.valueOf("portrait_info_fql_test");

//        TableDescriptor portraitInfoFqlTest1 = TableDescriptorBuilder
//                .newBuilder(TableName.valueOf("portrait_info_fql_test_1"))
//                .setCoprocessor(HbaseTimeoutParamCoprocessor.class.getName())
//                .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY_NAME))
//                .build();

        hbaseConf.set("hbase.rpc.timeout", "2000");
        hbaseConf.set("hbase.rpc.read.timeout", "2000");
        hbaseConf.set("hbase.client.pause", "1000");
        hbaseConf.set("hbase.client.retries.number", "2");
        hbaseConf.set("hbase.client.operation.timeout", "50000");


        try (Connection connection = ConnectionFactory.createConnection(hbaseConf);
             Table table = connection.getTableBuilder(tableName, null)
//                     .setRpcTimeout(100)
//                     .setReadRpcTimeout(100)
//                     .setWriteRpcTimeout(100)
//                     .setOperationTimeout(100)
                     .build()) {
            Admin admin = connection.getAdmin();
            for (TableName name : admin.listTableNames()) {
                System.out.println("表有: " + name.getNameAsString());
            }

            Get get = new Get(getRowKey(100121));

            byte[][] columns = getLabelColumn(Lists.newArrayList(8));

            for (byte[] column : columns) {
                get.addColumn(FAMILY_NAME.getBytes(), column);
            }

            Result result = table.get(get);

            Map<String, Cell> colCell = new HashMap<>();
            for (byte[] column : columns) {
                Cell cell = result.getColumnLatestCell(FAMILY_NAME.getBytes(), column);
                String qualifier = Bytes.toString(column);
                colCell.put(qualifier, cell);
            }

            colCell.forEach((k, v) -> {
                if (v == null) {
                    System.err.println("没有值");
                } else {
                    String value = Bytes.toString(CellUtil.cloneValue(v));
                    System.out.println("最后得到的值是: " + value);
                }

            });


        } catch (Exception e) {
            e.printStackTrace();
        }

//
//
//
//        Table table = connection.getTable(TableName.valueOf("aaaa"));
//
//


    }

    private static byte[] getRowKey(Integer id) {
        return ((id % 150 + 100) +
                VERTICAL_LINE +
                id).getBytes();

    }

    private static byte[][] getLabelColumn(List<Integer> labelIds) {
        byte[][] columns = new byte[labelIds.size()][];
        for (int i = 0; i < labelIds.size(); i++) {
            columns[i] = ("f" + labelIds.get(i)).getBytes();
        }
        return columns;
    }

}
